#ifndef BXC_RTSPSERVER_EVENT_H
#define BXC_RTSPSERVER_EVENT_H
typedef void (*EventCallback)(void*);

//触发事件
class TriggerEvent{
public:
    static TriggerEvent* createNew(void* arg);
    static TriggerEvent* createNew();

    TriggerEvent(void* arg);
    ~TriggerEvent();

    void setArg(void* arg) { mArg = arg; }

    void setTriggerCallback(EventCallback cb) { mTriggerCallback = cb; }

    void handleEvent();

private:
    void* mArg;
    EventCallback mTriggerCallback;
};

//定时器事件
class TimerEvent{
public:
    static TimerEvent* createNew(void* arg);
    static TimerEvent* createNew();

    TimerEvent(void* arg);
    ~TimerEvent();

    void setArg(void* arg) { mArg = arg; }

    void setTimeoutCallback(EventCallback cb) { mTimeoutCallback = cb; }

    bool handleEvent();

    void stop();

private:
    void* mArg;

    EventCallback mTimeoutCallback;

    bool mIsStop;
};

//io事件
class IOEvent{
public:
    enum IOEventType
    {
        EVENT_NONE = 0,
        EVENT_READ = 1,
        EVENT_WRITE = 2,
        EVENT_ERROR = 4,
    };
    
    static IOEvent* createNew(int fd, void* arg);
    static IOEvent* createNew(int fd);

    IOEvent(int fd, void* arg);
    ~IOEvent();

    int getFd() const { return mFd; }

    int getEvent() const { return mEvent; }
    void setREvent(int event) { mREvent = event; }

    void setArg(void* arg) { mArg = arg; }

    void setReadCallback(EventCallback cb) { mReadCallback = cb; };
    void setWriteCallback(EventCallback cb) { mWriteCallback = cb; };
    void setErrorCallback(EventCallback cb) { mErrorCallback = cb; };

    void enableReadHandling() { mEvent |= EVENT_READ; }
    void enableWriteHandling() { mEvent |= EVENT_WRITE; }
    void enableErrorHandling() { mEvent |= EVENT_ERROR; }

    void disableReadeHandling() { mEvent &= ~EVENT_READ; }
    void disableWriteHandling() { mEvent &= ~EVENT_WRITE; }
    void disableErrorHandling() { mEvent &= ~EVENT_ERROR; }

    bool isNoneHandling() const { return mEvent == EVENT_NONE; }
    bool isReadHandling() const { return (mEvent & EVENT_READ) != 0; }
    bool isWriteHandling() const { return (mEvent & EVENT_WRITE) != 0; }
    bool isErrorHandling() const { return (mEvent & EVENT_ERROR) != 0; };

    void handleEvent();

private:
    int mFd;//描述符

    void* mArg;

    int mEvent;
    int mREvent;

    EventCallback mReadCallback;//使用函数指针，可以动态调用不同方法执行
    EventCallback mWriteCallback;//C++中使用函数指针，可以让对象持有指针，暴露设置指针的方法，或者使用静态函数，C语言中使用函数指针简单很多
    EventCallback mErrorCallback;
};

#endif //BXC_RTSPSERVER_EVENT_H